home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 151-175 / disk_166 / stevie / source / main.c < prev    next >
C/C++ Source or Header  |  1992-05-06  |  9KB  |  346 lines

  1. /*
  2.  * STEVIE - Simply Try this Editor for VI Enthusiasts
  3.  *
  4.  * Code Contributions By : Tim Thompson           twitch!tjt
  5.  *                         Tony Andrews           onecom!wldrdg!tony 
  6.  *                         G. R. (Fred) Walter    watmath!watcgl!grwalter 
  7.  */
  8.  
  9. #include "stevie.h"
  10.  
  11. int             Rows;        /* Number of Rows and Columns */
  12. int             Columns;    /* in the current window. */
  13.  
  14. char           *Realscreen = NULL;    /* What's currently on the screen, a
  15.                      * single array of size Rows*Columns. */
  16. char           *Nextscreen = NULL;    /* What's to be put on the screen. */
  17.  
  18. char           *Filename = NULL;/* Current file name */
  19.  
  20. LPTR           *Filemem;    /* The contents of the file, as a single
  21.                  * array. */
  22.  
  23. LPTR           *Fileend;    /* Pointer to the end of the file in Filemem.
  24.                  * (It points to the byte AFTER the last
  25.                  * byte.) */
  26.  
  27. LPTR           *Topchar;    /* Pointer to the byte in Filemem which is in
  28.                  * the upper left corner of the screen. */
  29.  
  30. LPTR           *Botchar;    /* Pointer to the byte in Filemem which is
  31.                  * just off the bottom of the screen. */
  32.  
  33. LPTR           *Curschar;    /* Pointer to byte in Filemem at which the
  34.                  * cursor is currently placed. */
  35.  
  36. int             Curscol;    /* Current position of cursor (column) */
  37. int             Cursrow;    /* Current position of cursor (row) */
  38.  
  39. int             Cursvcol;    /* Current virtual column, the column number
  40.                  * of the file's actual line, as opposed to
  41.                  * the column number we're at on the screen.
  42.                  * This makes a difference on lines that span
  43.                  * more than one screen line. */
  44.  
  45. int             Curswant = 0;    /* The column we'd like to be at. This is
  46.                  * used try to stay in the same column
  47.                  * through up/down cursor motions. */
  48.  
  49. bool_t          set_want_col;    /* If set, then update Curswant the next time
  50.                  * through cursupdate() to the current
  51.                  * virtual column. */
  52.  
  53. int             State = NORMAL;    /* This is the current state of the command
  54.                  * interpreter. */
  55.  
  56. int             Prenum = 0;    /* The (optional) number before a command. */
  57.  
  58. LPTR           *Insstart;    /* This is where the latest insert/append
  59.                  * mode started. */
  60.  
  61. bool_t          Changed = FALSE;/* Set to TRUE if something in the file has
  62.                  * been changed and not written out. */
  63.  
  64. char           *IObuff;        /* file reads are done, one line at a time,
  65.                  * into this buffer; as well as sprintf's */
  66.  
  67. char           *Insbuffptr = NULL;
  68. char           *Insbuff;    /* Each insertion gets stuffed into this
  69.                  * buffer. */
  70.  
  71. char           *Readbuffptr = NULL;
  72. char           *Readbuff;    /* Having this buffer allows STEVIE to easily
  73.                  * make itself do commands */
  74.  
  75. char           *Redobuffptr = NULL;
  76. char           *Redobuff;    /* Each command should stuff characters into
  77.                  * this buffer that will re-execute itself. */
  78.  
  79. bool_t          UndoInProgress = FALSE;    /* Set to TRUE if undo'ing */
  80. char           *Undobuffptr = NULL;
  81. char           *Undobuff;    /* Each command should stuff characters into
  82.                  * this buffer that will undo its effects. */
  83.  
  84. char           *UndoUndobuffptr = NULL;
  85. char           *UndoUndobuff;    /* Each command should stuff characters into
  86.                  * this buffer that will undo its undo. */
  87.  
  88. char           *Yankbuffptr = NULL;
  89. char           *Yankbuff;    /* Yank buffer */
  90.  
  91. char            last_command = NUL;    /* last command */
  92. char            last_command_char = NUL;    /* character needed to undo
  93.                          * last command */
  94.  
  95. bool_t          RedrawingDisabled = FALSE;    /* Set to TRUE if undo'ing or
  96.                          * put'ing */
  97.  
  98. bool_t          MustRedrawLine = FALSE;    /* Set to TRUE if we must redraw the
  99.                      * current line */
  100. bool_t          MustRedrawScreen = TRUE;    /* Set to TRUE if we must
  101.                          * redraw the screen */
  102.  
  103. char          **files;        /* list of input files */
  104. int             numfiles;    /* number of input files */
  105. int             curfile;    /* number of the current file */
  106.  
  107. static void
  108. usage()
  109. {
  110.     fprintf(stderr, "usage: stevie [file ...]\n");
  111.     fprintf(stderr, "       stevie -t tag\n");
  112.     fprintf(stderr, "       stevie +[num] file\n");
  113.     fprintf(stderr, "       stevie +/pat  file\n");
  114.     exit(1);
  115. }
  116.  
  117. #ifdef AMIGA
  118. void
  119. #else
  120. int
  121. #endif
  122. main(argc, argv)
  123.     int             argc;
  124.     char          **argv;
  125. {
  126.     char           *initstr, *getenv();    /* init string from the environment */
  127.     char           *tag = NULL;    /* tag from command line */
  128.     char           *pat = NULL;    /* pattern from command line */
  129.     int             line = -1;    /* line number from command line */
  130.  
  131.     int             atoi();
  132.  
  133. #ifdef AMIGA
  134. /*
  135.  * This won't be needed if you have a version of Lattice 4.01 without broken
  136.  * break signal handling.
  137.  */
  138.     (void) signal(SIGINT, SIG_IGN);
  139. #endif
  140.  
  141.     /*
  142.      * Process the command line arguments. 
  143.      */
  144.     if (argc > 1) {
  145.     switch (argv[1][0]) {
  146.  
  147.       case '-':        /* -t tag */
  148.         if (argv[1][1] != 't')
  149.         usage();
  150.  
  151.         if (argv[2] == NULL)
  152.         usage();
  153.  
  154.         Filename = NULL;
  155.         tag = argv[2];
  156.         numfiles = 1;
  157.         break;
  158.  
  159.       case '+':        /* +n or +/pat */
  160.         if (argv[1][1] == '/') {
  161.         if (argv[2] == NULL)
  162.             usage();
  163.         Filename = strsave(argv[2]);
  164.         pat = &(argv[1][1]);
  165.         numfiles = 1;
  166.  
  167.         } else if (isdigit(argv[1][1]) || argv[1][1] == NUL) {
  168.         if (argv[2] == NULL)
  169.             usage();
  170.         Filename = strsave(argv[2]);
  171.         numfiles = 1;
  172.  
  173.         line = (isdigit(argv[1][1])) ?
  174.             atoi(&(argv[1][1])) : 0;
  175.         } else
  176.         usage();
  177.  
  178.         break;
  179.  
  180.       default:        /* must be a file name */
  181.         Filename = strsave(argv[1]);
  182.         files = &(argv[1]);
  183.         numfiles = argc - 1;
  184.         break;
  185.     }
  186.     } else {
  187.     Filename = NULL;
  188.     numfiles = 1;
  189.     }
  190.     curfile = 0;
  191.  
  192.     windinit();
  193.  
  194.     /*
  195.      * Allocate LPTR structures for all the various position pointers 
  196.      */
  197.     if ((Filemem = (LPTR *) malloc((unsigned) sizeof(LPTR))) == NULL) {
  198.     fprintf(stderr, "Can't allocate data structures\n");
  199.     windexit(0);
  200.     }
  201.     if ((Fileend = (LPTR *) malloc((unsigned) sizeof(LPTR))) == NULL) {
  202.     fprintf(stderr, "Can't allocate data structures\n");
  203.     windexit(0);
  204.     }
  205.     if ((Topchar = (LPTR *) malloc((unsigned) sizeof(LPTR))) == NULL) {
  206.     fprintf(stderr, "Can't allocate data structures\n");
  207.     windexit(0);
  208.     }
  209.     if ((Botchar = (LPTR *) malloc((unsigned) sizeof(LPTR))) == NULL) {
  210.     fprintf(stderr, "Can't allocate data structures\n");
  211.     windexit(0);
  212.     }
  213.     if ((Curschar = (LPTR *) malloc((unsigned) sizeof(LPTR))) == NULL) {
  214.     fprintf(stderr, "Can't allocate data structures\n");
  215.     windexit(0);
  216.     }
  217.     if ((Insstart = (LPTR *) malloc((unsigned) sizeof(LPTR))) == NULL) {
  218.     fprintf(stderr, "Can't allocate data structures\n");
  219.     windexit(0);
  220.     }
  221.     /*
  222.      * Allocate space for the many buffers 
  223.      */
  224.     if ((IObuff = malloc(IOSIZE)) == NULL) {
  225.     fprintf(stderr, "Can't allocate data structures\n");
  226.     windexit(0);
  227.     }
  228.     if ((Insbuff = malloc(INSERT_SIZE)) == NULL) {
  229.     fprintf(stderr, "Can't allocate data structures\n");
  230.     windexit(0);
  231.     }
  232.     if ((Readbuff = malloc(READSIZE)) == NULL) {
  233.     fprintf(stderr, "Can't allocate data structures\n");
  234.     windexit(0);
  235.     }
  236.     if ((Redobuff = malloc(REDO_UNDO_SIZE)) == NULL) {
  237.     fprintf(stderr, "Can't allocate data structures\n");
  238.     windexit(0);
  239.     }
  240.     if ((Undobuff = malloc(REDO_UNDO_SIZE)) == NULL) {
  241.     fprintf(stderr, "Can't allocate data structures\n");
  242.     windexit(0);
  243.     }
  244.     if ((UndoUndobuff = malloc(REDO_UNDO_SIZE)) == NULL) {
  245.     fprintf(stderr, "Can't allocate data structures\n");
  246.     windexit(0);
  247.     }
  248.     if ((Yankbuff = malloc(YANKSIZE)) == NULL) {
  249.     fprintf(stderr, "Can't allocate data structures\n");
  250.     windexit(0);
  251.     }
  252.     screenalloc();
  253.     filealloc();        /* Initialize Filemem & Fileend */
  254.  
  255.     screenclear();
  256.  
  257.     if ((initstr = getenv("EXINIT")) != NULL) {
  258.     char           *lp, buf[128];
  259.  
  260.     if ((lp = getenv("LINES")) != NULL) {
  261.         sprintf(buf, "%s lines=%s", initstr, lp);
  262.         readcmdline(':', buf);
  263.     } else
  264.         readcmdline(':', initstr);
  265.     }
  266.     if (Filename != NULL) {
  267.     if (readfile(Filename, Filemem, FALSE))
  268.         filemess("[New File]");
  269.     } else
  270.     msg("Empty Buffer");
  271.  
  272.     setpcmark();
  273.  
  274.     updateNextscreen();
  275.  
  276.     if (tag) {
  277.     stuffReadbuff(":ta ");
  278.     stuffReadbuff(tag);
  279.     stuffReadbuff("\n");
  280.  
  281.     } else if (pat) {
  282.     stuffReadbuff(pat);
  283.     stuffReadbuff("\n");
  284.  
  285.     } else if (line >= 0) {
  286.     if (line > 0)
  287.         stuffnumReadbuff(line);
  288.     stuffReadbuff("G");
  289.     }
  290.     edit();
  291.  
  292.     windexit(0);
  293. }
  294.  
  295. void
  296. stuffReadbuff(s)
  297.     char           *s;
  298. {
  299.     if (strlen(s) == 0)
  300.     return;
  301.  
  302.     if (Readbuffptr == NULL) {
  303.     if ((strlen(s) + 1) < READSIZE) {
  304.         strcpy(Readbuff, s);
  305.         Readbuffptr = Readbuff;
  306.     } else
  307.         emsg("Couldn't stuffReadbuff() - should never happen");
  308.     } else if ((strlen(Readbuff) + (strlen(s) + 1)) < READSIZE)
  309.     strcat(Readbuff, s);
  310.     else
  311.     emsg("Couldn't stuffReadbuff() - should never happen");
  312. }
  313.  
  314. void
  315. stuffnumReadbuff(n)
  316.     int             n;
  317. {
  318.     char            buf[32];
  319.  
  320.     sprintf(buf, "%d", n);
  321.     stuffReadbuff(buf);
  322. }
  323.  
  324. /* OPTRESULT */
  325. char
  326. vgetc()
  327. {
  328.     if (Readbuffptr != NULL) {
  329.     char            nextc = *Readbuffptr++;
  330.     if (*Readbuffptr == NUL) {
  331.         *Readbuff = NUL;
  332.         Readbuffptr = NULL;
  333.     }
  334.     return (nextc);
  335.     }
  336.     return (inchar());
  337. }
  338.  
  339. char
  340. vpeekc()
  341. {
  342.     if (Readbuffptr != NULL)
  343.     return (*Readbuffptr);
  344.     return (NUL);
  345. }
  346.